package com.xueqiu.movieplaymanagersystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xueqiu.movieplaymanagersystem.entity.Tag;
import com.xueqiu.movieplaymanagersystem.mapper.TagMapper;
import com.xueqiu.movieplaymanagersystem.service.BlogTagService;
import com.xueqiu.movieplaymanagersystem.service.TagService;
import com.xueqiu.movieplaymanagersystem.utils.UUIDUtils;

import java.util.List;

/**
 * @author xueqiu
 */
@Service("tagService")
@Slf4j
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private BlogTagService blogTagService;

    /**
     * 新增
     *
     * @param originData 数据源
     * @return 新增对象；含ID
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Tag save(Tag originData) {
        //先查询数据库中是否已存在
        Tag originTag = tagMapper.selectByName(originData.getTagName());

        if (originTag != null) {
            return null;
        }
        //id是使用UUID生成而不是自增
        String uuid = UUIDUtils.uuid();
        originData.setId(uuid);
        log.info("TagServiceImpl save originData ----> {}",originData);
        return tagMapper.insert(originData) >= 1 ? originData : null;
    }

    /**
     * 编辑
     *
     * @param id   id字段
     * @param name TAG/TYPE名称
     * @return 编辑成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(String id, String name) {
        int update = tagMapper.update(id, name);
        log.info("TagServiceImpl edit id name ----> {},{}",id,name);
        return update >= 1;
    }

    /**
     * 删除
     *
     * @param id id字段
     * @return 删除成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(String id) {
        int deleteTag = tagMapper.deleteById(id);
        int deletedBlogTag = blogTagService.removeByTagId(id);
        return deleteTag >= 1 && deletedBlogTag >= 1;
    }

    /**
     * 批量删除
     *
     * @param idList id字段列表
     * @return 删除条数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByNameBatch(List<String> idList) {
        int deleteTags = tagMapper.deleteByIdBatch(idList);
        int deletedBlogTags = blogTagService.removeByTagIdBatch(idList);
        return deleteTags + deletedBlogTags;
    }

    /**
     * 分页查询，条件查询
     *
     * @param keyWord 关键字
     * @param page    当前页
     * @param limit   每页条数
     * @return 结果可能为empty
     */
    @Override
    @Transactional(readOnly = true)
    public PageInfo<Tag> findByKeyWord(String keyWord, int page, int limit) {
        if (page == -1 || limit == -1) {
            List<Tag> originList = tagMapper.selectByNameKeyWord(keyWord);

            PageInfo<Tag> resultPageInfo = new PageInfo<>(originList);
            resultPageInfo.setTotal(originList.size());
            return resultPageInfo;
        }
        PageInfo<Tag> resultPageInfo = null;
        try (Page<Tag> originPage = PageHelper.startPage(page, limit)) {
            tagMapper.selectByNameKeyWord(keyWord);

            List<Tag> originList = originPage.getResult();
            resultPageInfo = new PageInfo<>(originList);
        }
        log.info("TagServiceImpl findByKeyWord resultPageInfo ----> {}",resultPageInfo);
        return resultPageInfo;
    }

    /**
     * 查询；包含每个标签下的新闻数量(blog_count)，并且只返回新闻数量大于0的标签
     * @return 返回新闻数量大于0的标签；可能为EMPTY
     */
    @Override
    @Transactional(readOnly = true)
    public List<Tag> findWithCount() {
        return tagMapper.selectWithCount();
    }

    /**
     * 查询；标签总数量
     * @return 标签总数量
     */
    @Override
    @Transactional(readOnly = true)
    public Integer findCount() {
        Integer count = tagMapper.selectCount();
        log.info("TagServiceImpl findCount count ----> {}",count);
        return count;
    }
}
